Micron Document
πŸŽ–οΈGitΠ―Ρ€Π°πŸŽ–οΈ


Displaying Raw β€’ Download

core/model/src/commonMain/kotlin/org/meshtastic/core/model/util/ChannelSet.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 10.20 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tf0883e@fileTb4b4b4:Te6edf3SuppressTb4b4b4(Ta5d6ff"Ta5d6ffMagicNumberTa5d6ff"Tb4b4b4)

Tff7b72package T7ee787org.meshtastic.core.model.util

Tff7b72import T7ee787okio.ByteString.Companion.decodeBase64
Tff7b72import T7ee787okio.ByteString.Companion.toByteString
Tff7b72import T7ee787org.meshtastic.core.common.util.CommonUri
Tff7b72import T7ee787org.meshtastic.core.model.Channel
Tff7b72import T7ee787org.meshtastic.core.model.channelNum
Tff7b72import T7ee787org.meshtastic.core.model.numChannels
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.Config.LoRaConfig
Tff7b72import T7ee787org.meshtastic.proto.Config.LoRaConfig.RegionCode
Tff7b72import T7ee787org.meshtastic.proto.MeshBeacon
Tff7b72import T7ee787org.meshtastic.proto.ModuleSettings

T8b949e/**
* Return a [ChannelSet] that represents the ChannelSet encoded by the URL.
*
* @throws MalformedMeshtasticUrlException when not recognized as a valid Meshtastic URL
*/
Tf0883e@ThrowsTb4b4b4(Te6edf3MalformedMeshtasticUrlExceptionTff7b72::Te6edf3classTb4b4b4)
Tff7b72fun Te6edf3CommonUriTb4b4b4.Td2a8fftoChannelSetTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ChannelSet Tb4b4b4{
Tff7b72val Te6edf3h Tff7b72= Te6edf3host Tff7b72?: Ta5d6ff"Ta5d6ff"
Tff7b72val Te6edf3isCorrectHost Tff7b72=
Te6edf3hTb4b4b4.Te6edf3equalsTb4b4b4(Te6edf3MESHTASTIC_HOSTTb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4) Tff7b72|Tff7b72| Te6edf3hTb4b4b4.Te6edf3equalsTb4b4b4(Ta5d6ff"Ta5d6ffwww.Tffd700$Te6edf3MESHTASTIC_HOSTTa5d6ff"Tb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4)
Tff7b72val Te6edf3segments Tff7b72= Te6edf3pathSegments
Tff7b72val Te6edf3isCorrectPath Tff7b72= Te6edf3segmentsTb4b4b4.Te6edf3any Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3equalsTb4b4b4(Ta5d6ff"Ta5d6ffeTa5d6ff"Tb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4}
Tff7b72val Te6edf3hasFragment Tff7b72= Tff7b72!Te6edf3fragmentTb4b4b4.Te6edf3isNullOrBlankTb4b4b4(Tb4b4b4)

Tff7b72if Tb4b4b4(Tff7b72!Te6edf3hasFragment Tff7b72|Tff7b72| Tff7b72!Te6edf3isCorrectHost Tff7b72|Tff7b72| Tff7b72!Te6edf3isCorrectPathTb4b4b4) Tb4b4b4{
Tff7b72throw Te6edf3MalformedMeshtasticUrlExceptionTb4b4b4(
Ta5d6ff"Ta5d6ffNot a valid Meshtastic URL: host=Tffd700$Te6edf3hTa5d6ff, segmentCount=Tffd700${Te6edf3segmentsTb4b4b4.Te6edf3sizeTffd700}Ta5d6ff, hasFragment=Tffd700$Te6edf3hasFragmentTa5d6ff"Tb4b4b4,
Tb4b4b4)
Tb4b4b4}

T8b949e// Older versions of Meshtastic clients (Apple/web) included `?add=true` within the URL fragment.
T8b949e// This gracefully handles those cases until the newer version are generally available/used.
Tff7b72val Te6edf3fragmentBase64 Tff7b72= Te6edf3fragmentTff7b72!!Tb4b4b4.Te6edf3substringBeforeTb4b4b4(Ta5d6ff'?'Tb4b4b4)Tb4b4b4.Te6edf3replaceTb4b4b4(Ta5d6ff'-'Tb4b4b4, Ta5d6ff'+'Tb4b4b4)Tb4b4b4.Te6edf3replaceTb4b4b4(Ta5d6ff'_'Tb4b4b4, Ta5d6ff'/'Tb4b4b4)
Tff7b72val Te6edf3fragmentBytes Tff7b72=
Te6edf3fragmentBase64Tb4b4b4.Te6edf3decodeBase64Tb4b4b4(Tb4b4b4) Tff7b72?: Tff7b72throw Te6edf3MalformedMeshtasticUrlExceptionTb4b4b4(Ta5d6ff"Ta5d6ffInvalid Base64 in URL fragmentTa5d6ff"Tb4b4b4)
Tff7b72val Te6edf3url Tff7b72= Te6edf3ChannelSetTb4b4b4.Te6edf3ADAPTERTb4b4b4.Te6edf3decodeTb4b4b4(Te6edf3fragmentBytesTb4b4b4)
Tff7b72val Te6edf3shouldAdd Tff7b72= Te6edf3fragmentTff7b72?.Te6edf3substringAfterTb4b4b4(Ta5d6ff'?'Tb4b4b4, Ta5d6ff"Ta5d6ff"Tb4b4b4)Tff7b72?.Te6edf3addParameterTb4b4b4(Tb4b4b4) Tff7b72?: Te6edf3getBooleanQueryParameterTb4b4b4(Ta5d6ff"Ta5d6ffaddTa5d6ff"Tb4b4b4, Tff7b72falseTb4b4b4)

Tff7b72return Tff7b72if Tb4b4b4(Te6edf3shouldAddTb4b4b4) Te6edf3urlTb4b4b4.Te6edf3copyTb4b4b4(Te6edf3lora_config Tff7b72= Tff7b72nullTb4b4b4) Tff7b72else Te6edf3url
Tb4b4b4}

Tff7b72private Tff7b72fun Te6edf3StringTb4b4b4.Td2a8ffaddParameterTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean? Tff7b72= Te6edf3splitTb4b4b4(Ta5d6ff'&'Tb4b4b4)
Tb4b4b4.Te6edf3firstOrNull Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3substringBeforeTb4b4b4(Ta5d6ff'='Tb4b4b4)Tb4b4b4.Te6edf3equalsTb4b4b4(Ta5d6ff"Ta5d6ffaddTa5d6ff"Tb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4}
Tff7b72?.Te6edf3substringAfterTb4b4b4(Ta5d6ff'='Tb4b4b4, Te6edf3missingDelimiterValue Tff7b72= Ta5d6ff"Ta5d6fftrueTa5d6ff"Tb4b4b4)
Tff7b72?.Te6edf3equalsTb4b4b4(Ta5d6ff"Ta5d6fftrueTa5d6ff"Tb4b4b4, Te6edf3ignoreCase Tff7b72= Tff7b72trueTb4b4b4)

T8b949e/** @return A list of globally unique channel IDs usable with MQTT subscribe() */
Tff7b72val Te6edf3ChannelSetTb4b4b4.Te6edf3subscribeListTb4b4b4: Te6edf3ListTff7b72<Tffa657StringTff7b72>
Tff7b72getTb4b4b4(Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3loraConfig Tff7b72= Tff7b72thisTb4b4b4.Te6edf3lora_config Tff7b72?: Te6edf3LoRaConfigTb4b4b4(Tb4b4b4)
Tff7b72return Te6edf3settingsTb4b4b4.Te6edf3filter Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3downlink_enabled Tb4b4b4}Tb4b4b4.Te6edf3map Tb4b4b4{ Te6edf3ChannelTb4b4b4(Tffa657itTb4b4b4, Te6edf3loraConfigTb4b4b4)Tb4b4b4.Te6edf3name Tb4b4b4}
Tb4b4b4}

Tff7b72fun Te6edf3ChannelSetTb4b4b4.Td2a8ffgetChannelTb4b4b4(Te6edf3indexTb4b4b4: Tffa657IntTb4b4b4)Tb4b4b4: Te6edf3Channel? Tff7b72= Tff7b72if Tb4b4b4(Te6edf3settingsTb4b4b4.Te6edf3size Tff7b72> Te6edf3indexTb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3s Tff7b72= Te6edf3settingsTff7b72[Te6edf3indexTff7b72]
Te6edf3ChannelTb4b4b4(Te6edf3sTb4b4b4, Te6edf3lora_config Tff7b72?: Te6edf3LoRaConfigTb4b4b4(Tb4b4b4)Tb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}

T8b949e/** Return the primary channel info */
Tff7b72val Te6edf3ChannelSetTb4b4b4.Te6edf3primaryChannelTb4b4b4: Te6edf3Channel?
Tff7b72getTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3getChannelTb4b4b4(T79c0ff0Tb4b4b4)

Tff7b72fun Te6edf3ChannelSetTb4b4b4.Td2a8ffhasLoraConfigTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657Boolean Tff7b72= Te6edf3lora_config Tff7b72!Tff7b72= Tff7b72null

T8b949e/** How a received Mesh Beacon invitation can be joined, given the connected radio's current settings. */
Tff7b72enum Tff7b72class T56d364BeaconJoinOption Tb4b4b4{
T8b949e/** Add the offered channel to a free secondary slot with no retune/reboot (mesh shares the radio's frequency). */
Te6edf3ADDTb4b4b4,

T8b949e/** Retune the radio's primary channel (and LoRa preset/region) onto the offered mesh β€” reboots the radio. */
Te6edf3SWITCHTb4b4b4,

T8b949e/** The beacon carries no join offer (message-only) β€” nothing to join. */
Te6edf3NONETb4b4b4,
Tb4b4b4}

T8b949e/**
* Decides whether a beacon can be joined by simply **adding** its channel (no reboot) or requires a **switch**
* (retune + reboot). Adding works only when the offered mesh sits on the radio's *current* frequency slot β€” Meshtastic
* secondary channels ride the primary channel's frequency, so the offered channel must resolve (name-hash) to the same
* slot the radio's primary is on, under a matching preset and region. Mirrors the Apple `014-mesh-beacons`
* FR-016/FR-017 logic.
*
* @param currentLora The radio's current [LoRaConfig] (`null` β†’ can't reason, so [SWITCH]).
* @param currentChannels The radio's current channel settings, index 0 = primary.
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffReturnCountTa5d6ff"Tb4b4b4)
Tff7b72fun Te6edf3MeshBeaconTb4b4b4.Td2a8ffbeaconJoinOptionTb4b4b4(Te6edf3currentLoraTb4b4b4: Te6edf3LoRaConfig?Tb4b4b4, Te6edf3currentChannelsTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3BeaconJoinOption Tb4b4b4{
Tff7b72val Te6edf3offer Tff7b72= Te6edf3offer_channel Tff7b72?: Tff7b72return Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3NONE
Tff7b72val Te6edf3lora Tff7b72= Te6edf3currentLora Tff7b72?: Tff7b72return Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3SWITCH
T8b949e// An offered preset must match the radio's; an omitted preset (null) means "ride the current preset" β†’ matches.
Tff7b72val Te6edf3presetMatches Tff7b72= Te6edf3offer_preset Tff7b72=Tff7b72= Tff7b72null Tff7b72|Tff7b72| Tb4b4b4(Te6edf3loraTb4b4b4.Te6edf3use_preset Tff7b72&Tff7b72& Te6edf3offer_preset Tff7b72=Tff7b72= Te6edf3loraTb4b4b4.Te6edf3modem_presetTb4b4b4)
T8b949e// offer_region == UNSET (0) means "not offered"; only a set, differing region forces a switch.
Tff7b72val Te6edf3regionMatches Tff7b72= Te6edf3offer_region Tff7b72=Tff7b72= Te6edf3RegionCodeTb4b4b4.Te6edf3UNSET Tff7b72|Tff7b72| Te6edf3offer_region Tff7b72=Tff7b72= Te6edf3loraTb4b4b4.Te6edf3region
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3presetMatches Tff7b72|Tff7b72| Tff7b72!Te6edf3regionMatchesTb4b4b4) Tff7b72return Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3SWITCH
T8b949e// With an explicit slot override we can't compare the offered mesh's slot; be safe and switch.
Tff7b72if Tb4b4b4(Te6edf3loraTb4b4b4.Te6edf3channel_num Tff7b72!Tff7b72= T79c0ff0 Tff7b72|Tff7b72| Te6edf3loraTb4b4b4.Te6edf3numChannels Tff7b72<Tff7b72= T79c0ff0Tb4b4b4) Tff7b72return Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3SWITCH
T8b949e// Hash the *effective* names: an empty channel name resolves to its preset display name ("LongFast", …), which is
T8b949e// what firmware hashes for the slot β€” comparing raw "" on both sides would misclassify an unnamed primary.
Tff7b72val Te6edf3currentSlot Tff7b72= Te6edf3loraTb4b4b4.Te6edf3channelNumTb4b4b4(Te6edf3ChannelTb4b4b4(Te6edf3currentChannelsTb4b4b4.Te6edf3firstOrNullTb4b4b4(Tb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4, Te6edf3loraTb4b4b4)Tb4b4b4.Te6edf3nameTb4b4b4)
Tff7b72val Te6edf3offeredSlot Tff7b72= Te6edf3loraTb4b4b4.Te6edf3channelNumTb4b4b4(Te6edf3ChannelTb4b4b4(Te6edf3offerTb4b4b4, Te6edf3loraTb4b4b4)Tb4b4b4.Te6edf3nameTb4b4b4)
Tff7b72return Tff7b72if Tb4b4b4(Te6edf3offeredSlot Tff7b72=Tff7b72= Te6edf3currentSlotTb4b4b4) Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3ADD Tff7b72else Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3SWITCH
Tb4b4b4}

T8b949e/**
* Builds the [ChannelSet] to hand the QR channel-import dialog for a given [option].
*
* [ADD][BeaconJoinOption.ADD] omits `lora_config` so the dialog merges the offered channel into a free secondary slot
* with no reboot. [SWITCH][BeaconJoinOption.SWITCH] carries a **fresh** `lora_config` (not a copy of [currentLora])
* with `use_preset = true`, the advertised preset+region applied, and every RF field left at its default β€” notably
* `channel_num`/`override_frequency` zeroed so firmware re-derives the frequency from the offered channel name (Apple
* FR-006). Starting blank guarantees no stale slot/frequency pin (`channel_num`, `override_frequency`, manual
* bandwidth/spread_factor/coding_rate) from the old mesh survives the retune. Only `region` is carried from
* [currentLora] when the beacon doesn't advertise one β€” this config is sent as a full LoRaConfig replacement, and a
* zero region disables transmit; `hop_limit`/`tx_enabled` fall back to the app's standard defaults. Returns `null` for
* [NONE][BeaconJoinOption.NONE] or a beacon with no offered channel.
*
* Both paths strip position sharing from the offered channel ([withoutPositionSharing]) so joining a stranger's mesh
* never leaks our location β€” matching Apple's `joinBeaconMesh`/`addBeaconChannel`.
*
* @param currentLora The radio's current [LoRaConfig]; a switch reuses only its region/preset as fallbacks.
*/
Tff7b72fun Te6edf3MeshBeaconTb4b4b4.Td2a8fftoJoinChannelSetTb4b4b4(Te6edf3optionTb4b4b4: Te6edf3BeaconJoinOptionTb4b4b4, Te6edf3currentLoraTb4b4b4: Te6edf3LoRaConfig?Tb4b4b4)Tb4b4b4: Te6edf3ChannelSet? Tb4b4b4{
Tff7b72val Te6edf3offerChannel Tff7b72= Tb4b4b4(Te6edf3offer_channel Tff7b72?: Tff7b72return Tff7b72nullTb4b4b4)Tb4b4b4.Te6edf3withoutPositionSharingTb4b4b4(Tb4b4b4)
Tff7b72return Tff7b72when Tb4b4b4(Te6edf3optionTb4b4b4) Tb4b4b4{
Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3ADD Tff7b72-Tff7b72> Te6edf3ChannelSetTb4b4b4(Te6edf3settings Tff7b72= Te6edf3listOfTb4b4b4(Te6edf3offerChannelTb4b4b4)Tb4b4b4)

Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3SWITCH Tff7b72-Tff7b72> Tb4b4b4{
T8b949e// Start from the shared device default ([Channel.default] β€” use_preset=true, hop_limit/tx_enabled set,
T8b949e// every RF field blank) rather than a copy of the current config: any stale RF pin on the connected radio
T8b949e// β€” an explicit channel_num, override_frequency, or manual bandwidth/spread_factor/coding_rate β€” would
T8b949e// otherwise strand it on the old slot. use_preset + the default (zero) channel_num/override_frequency lets
T8b949e// firmware re-derive the frequency from the offered channel name (Apple FR-006). This is sent as a full
T8b949e// LoRaConfig replacement, so carry region (a zero region disables transmit); the beacon proto advertises
T8b949e// no other RF fields.
Tff7b72val Te6edf3base Tff7b72= Te6edf3currentLora Tff7b72?: Te6edf3LoRaConfigTb4b4b4(Tb4b4b4)
Tff7b72val Te6edf3loraConfig Tff7b72=
Te6edf3ChannelTb4b4b4.Te6edf3defaultTb4b4b4.Te6edf3loraConfigTb4b4b4.Te6edf3copyTb4b4b4(
Te6edf3modem_preset Tff7b72= Te6edf3offer_preset Tff7b72?: Te6edf3baseTb4b4b4.Te6edf3modem_presetTb4b4b4,
Te6edf3region Tff7b72= Tff7b72if Tb4b4b4(Te6edf3offer_region Tff7b72!Tff7b72= Te6edf3RegionCodeTb4b4b4.Te6edf3UNSETTb4b4b4) Te6edf3offer_region Tff7b72else Te6edf3baseTb4b4b4.Te6edf3regionTb4b4b4,
Tb4b4b4)
Te6edf3ChannelSetTb4b4b4(Te6edf3settings Tff7b72= Te6edf3listOfTb4b4b4(Te6edf3offerChannelTb4b4b4)Tb4b4b4, Te6edf3lora_config Tff7b72= Te6edf3loraConfigTb4b4b4)
Tb4b4b4}

Te6edf3BeaconJoinOptionTb4b4b4.Te6edf3NONE Tff7b72-Tff7b72> Tff7b72null
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Zeroes `position_precision` on the offered channel so joining a beaconed mesh never broadcasts our location to a mesh
* of strangers (privacy-first; Apple sets `positionPrecision = 0` on both add and switch).
*/
Tff7b72private Tff7b72fun Te6edf3ChannelSettingsTb4b4b4.Td2a8ffwithoutPositionSharingTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3ChannelSettings Tff7b72=
Te6edf3copyTb4b4b4(Te6edf3module_settings Tff7b72= Tb4b4b4(Te6edf3module_settings Tff7b72?: Te6edf3ModuleSettingsTb4b4b4(Tb4b4b4)Tb4b4b4)Tb4b4b4.Te6edf3copyTb4b4b4(Te6edf3position_precision Tff7b72= T79c0ff0Tb4b4b4)Tb4b4b4)

T8b949e/**
* Return a URL that represents the [ChannelSet]
*
* @param upperCasePrefix portions of the URL can be upper case to make for more efficient QR codes
*/
Tff7b72fun Te6edf3ChannelSetTb4b4b4.Td2a8ffgetChannelUrlTb4b4b4(Te6edf3upperCasePrefixTb4b4b4: Tffa657Boolean Tff7b72= Tff7b72falseTb4b4b4, Te6edf3shouldAddTb4b4b4: Tffa657Boolean Tff7b72= Tff7b72falseTb4b4b4)Tb4b4b4: Te6edf3CommonUri Tb4b4b4{
Tff7b72val Te6edf3channelBytes Tff7b72= Te6edf3ChannelSetTb4b4b4.Te6edf3ADAPTERTb4b4b4.Te6edf3encodeTb4b4b4(Tff7b72if Tb4b4b4(Te6edf3shouldAddTb4b4b4) Te6edf3copyTb4b4b4(Te6edf3lora_config Tff7b72= Tff7b72nullTb4b4b4) Tff7b72else Tff7b72thisTb4b4b4)
Tff7b72val Te6edf3enc Tff7b72= Te6edf3channelBytesTb4b4b4.Te6edf3toByteStringTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3base64UrlTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3replaceTb4b4b4(Ta5d6ff"Ta5d6ff=Ta5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ff"Tb4b4b4)
Tff7b72val Te6edf3p Tff7b72= Tff7b72if Tb4b4b4(Te6edf3upperCasePrefixTb4b4b4) Te6edf3CHANNEL_URL_PREFIXTb4b4b4.Te6edf3uppercaseTb4b4b4(Tb4b4b4) Tff7b72else Te6edf3CHANNEL_URL_PREFIX
Tff7b72val Te6edf3query Tff7b72= Tff7b72if Tb4b4b4(Te6edf3shouldAddTb4b4b4) Ta5d6ff"Ta5d6ff?add=trueTa5d6ff" Tff7b72else Ta5d6ff"Ta5d6ff"
Tff7b72return Te6edf3CommonUriTb4b4b4.Te6edf3parseTb4b4b4(Ta5d6ff"Tffd700$Te6edf3pTffd700$Te6edf3queryTa5d6ff#Tffd700$Te6edf3encTa5d6ff"Tb4b4b4)
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.07s